/*!
 * Copyright &copy; 2013-2015 山东易科德软件有限公司   All rights reserved.
 * 
 * 树列表公共方法
 * @author ThinkGem、刘保全
 * @version 2014-5-31
 */

/**
 * AJAX 加载树表格
 */
function ajaxLoadTreetable(searchForm, treeTable, treeTableList, treeTableTpl,  
		addTreetableRowCallback, successCallback, isDefaultExpand, defaultExpandLevel){
	
	ajaxSubmitForm(searchForm, function(data){
		// 清除原数据
		if (treeTable){
			treeTable.treetable('destroy');
		}
		treeTableList.empty();
		
		// 加载新数据
//		var rootIds = getTreetableRootIds(data);
//		for (var i=0; i < rootIds.length; i++){
//			addTreetableRow(treeTableList, treeTableTpl, data, rootIds[i], true, 0, function(row){
//				return addTreetableRowCallback(row);
//			});
//		}
		for (var i = 0; i < data.length; i++){
  			var row = data[i];
  			treeTableList.append(Mustache.render(treeTableTpl, {
				dict: addTreetableRowCallback(row), 
				pid: 0, isRoot: true, level: 1, row: row
			}));
  		}
		
		// 初始化树结构表格
		treeTable.treetable({
			expandable: true,
//			onNodeCollapse: function() {
//				var node = this;
//				treeTable.treetable("unloadBranch", node);
//			},
			onNodeExpand: function() {
				var node = this;
				if(node.children.length){
					return;
				}
				loading();
				setTimeout(function(){
					searchForm.find("#parentCodes, #code").val('');
					searchForm.find("#parentCode").val(node.id);
					ajaxSubmitForm(searchForm, function(data){
						if (data.length == 0){
							treeTable.find('tr[data-tt-id=' + node.id + ']')
								.removeAttr('data-tt-branch')
								.removeClass('branch').addClass('leaf')
								.find(".indenter").empty();
				  		}else{
					  		var rows = [];
					  		for (var i = 0; i < data.length; i++){
					  			var row = data[i];
					  			rows.push(Mustache.render(treeTableTpl, {
									dict: addTreetableRowCallback(row), 
									pid: node.id, isRoot: false, level: node.level()+2, row: row
								}));
					  		}
							treeTable.treetable("loadBranch", node, rows.join(''));
							successCallback(treeTable.find('tr[data-tt-parent-id=' + node.id + ']'));
				  		}
						searchForm.find("#parentCode").val('');
					}, 'json', true, 'none');
				}, 100);
			},
			stringExpand: "展开",
	        stringCollapse: "折叠"
	    });
		
		// 默认展开节点
		if (isDefaultExpand == undefined ? true : isDefaultExpand){
			expandTreetableByLevel(treeTable, defaultExpandLevel == undefined ? 1 : defaultExpandLevel);
		}

		// 绑定展开或折叠节点，重绘数据表
		$('.indenter', treeTable).click(function(){
			$(window).resize();
		});
		
		// 加载成功回调
		successCallback(treeTable);
	});
	return false;
}

///**
//* 获取所有跟节点ID
//*/
//function getTreetableRootIds(data){
//	var ids = [], rootIds = [];
//	for (var i=0; i<data.length; i++){
//		ids.push(data[i].id);
//	}
//	ids = ',' + ids.join(',') + ',';
//	for (var i=0; i<data.length; i++){
//		if (ids.indexOf(','+data[i].parentCode+',') == -1){
//			if ((','+rootIds.join(',')+',').indexOf(','+data[i].parentCode+',') == -1){
//				rootIds.push(data[i].parentCode);
//			}
//		}
//	}
//	return rootIds;
//}
//
///**
//* 添加树列表行
//*/
//function addTreetableRow(tbodyObj, tpl, dataList, pid, isRoot, level, dict){
//	for (var i = 0; i < dataList.length; i++){
//		var row = dataList[i];
//		if ((!row?'':!row.parentCode?'':row.parentCode) == pid){
//			tbodyObj.append(Mustache.render(tpl, {
//				dict: dict(row), pid: (isRoot ? 0 : pid), isRoot: isRoot, level: level+1, row: row
//			}));
//			addTreetableRow(tbodyObj, tpl, dataList, row.id, false, level+1, dict);
//		}
//	}
//}

/**
* 根据级别展开树
* @param level 展开节点的级别，最小为1
*/
function expandTreetableByLevel(treeTable, level){
	setTimeout(function(){
		for(var le = 1; le <= level; le++){
			var lObj = jQuery('tr[level='+le+']', treeTable);
			for(var i=0; i < lObj.length; i++){
				var eId = lObj.eq(i).data('ttId');
				treeTable.treetable("expandNode", eId);
			}
		}
	}, Metronic.isIE8() ? 500 : 0);
}

/**
* 展开全部
*/
function expandAllTreetable(treeTable){
	loading();
	setTimeout(function(){
		treeTable.treetable('expandAll');
		$(window).resize();
		closeLoading();
	}, 100);
	return false;
}
/**
* 折叠全部
*/
function collapseAllTreeTreetable(treeTable){
	loading();
	setTimeout(function(){
		treeTable.treetable('collapseAll');
		$(window).resize();
		closeLoading();
	}, 100);
	return false;
}